﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using System.Data;

namespace logic
{
	public class product
	{
        public static int insert(ProductEntity pro)
        {
            return Product.insert(pro);
        }

        public static void update(ProductEntity pro)
        {
            Product.update(pro);
        }

        public static void delete(int productId)
        {
            Product.delete(productId);
        }

        public static ProductEntity getById(int productId)
        {
            return Product.getById(productId);
        }

        public static DataTable getByCategory(int categoryId)
        {
            return Product.getByCategory(categoryId);
        }

        public static DataTable getByCategoryLimited(int categoryId,int companyId)
        {
            return Product.getByCategoryLimited(categoryId,companyId);
        }

        public static DataTable listWithPriceByCompanyAndCategory(int companyId, int categoryId,string pros)
        {
            return Product.listWithPriceByCompanyAndCategory(companyId, categoryId,pros);
        }

        public static DataTable listWithPriceByCompanyAndProsuctes(int companyId, int pros,string nature)
        {
            return Product.listWithPriceByCompanyAndProsuctes(companyId, pros, nature);
        }
        public static DataTable listWithPriceByCompanyAndProsuctes(int companyId, int pros, string nature,int fpid)
        {
            return Product.listWithPriceByCompanyAndProsuctes(companyId, pros, nature, fpid);
        }
        public static string getAutoProductNo(int categoryId)
        {
            return Product.getAutoProductNo(categoryId);
        }

        public static DataSet select(string where, string orderby, int pagesize, int pageindex)
        {
            return DAL.DbHelper.ExecuteDataSetPageSize("*", "vw_Product_list", where, "productId", orderby, pagesize, pageindex);
        }
        public static DataSet selectmyRecipe(string where, string orderby, int pagesize, int pageindex)
        {
            return DAL.DbHelper.ExecuteDataSetPageSize("*", "vw_ProductPrice_list", where, "productId", orderby, pagesize, pageindex);
        }

        public static DataSet listWithNewPrice(string where, string orderby, int pagesize, int pageindex)
        {
            return DAL.DbHelper.ExecuteDataSetPageSize("*", "vw_Product_listWithNewPrice", where, "productId", orderby, pagesize, pageindex);
        }

        public static DataSet listWithCurrentPrice(int companyId,string where, string orderby, int pagesize, int pageindex)
        {
            string newPrice = "CASE a.isAutoPrice WHEN 1 THEN a.sellerPrice*(1+a.autoAdd+(SELECT c.autoPercent FROM priceStructure c WHERE c.companyId=" 
                + companyId.ToString()+ " AND c.productId=a.productId))";
            newPrice += "ELSE a.price  END  AS newPrice  ";            
            return DAL.DbHelper.ExecuteDataSetPageSize("a.*,"+newPrice, "vw_Product_listWithCurrentPrice a", where, "productId", orderby, pagesize, pageindex);
        }

        public static bool isExistProductNo(int productId, string productNo)
        {
            return Product.isExistProductNo(productId, productNo); 
        }

        //public static DataSet listWithOrderItem(string where, string orderby, int pagesize, int pageindex)
        //{
        //    return DAL.DbHelper.ExecuteDataSetPageSize("*", "vw_Product_listWithOrderItem", where, "productId", orderby, pagesize, pageindex);
        //}

        public static DataSet listNotSupply(string where, string orderby, int pagesize, int pageindex)
        {
            return DAL.DbHelper.ExecuteDataSetPageSize("*", "vw_Product_listNotSupply", where, "productId", orderby, pagesize, pageindex);
        }

        /// <summary>
        /// 每个采购商采购商品的当前价格
        /// </summary>
        /// <param name="companyId">企业ID</param>
        /// <returns>DataTable</returns>
        public static DataTable listWithPriceByBuyer(int companyId)
        {
            return Product.listWithPriceByBuyer(companyId);
        }

        
        public static Decimal getBuyerPrice(int buyerId, decimal sellerPrice, int productId)
        {
            return Product.getBuyerPrice(buyerId, sellerPrice, productId);
        }

        public static void updateAutoAddByCategoryId(int categoryId, decimal autoAdd)
        {
            Product.updateAutoAddByCategoryId(categoryId, autoAdd);
        }


        public static DataTable listWithPriceByCompanyAndRecepe(int companyId, string recepe)
        {
            return Product.listWithPriceByCompanyAndRecepe(companyId, recepe);
        }
        public static int CountProduct()
        {
            return Product.CountProduct();
        }

        //查询出订单细目中的时价商品
        public static DataSet listWithCurrentcost(string where, string orderby, int pagesize, int pageindex)
        {
            return DAL.DbHelper.ExecuteDataSetPageSize("*", "vw_Product_listWithCurrentcost", where, "productId", orderby, pagesize, pageindex);
        }

        public static void copyInsert(int productId, string rename, string fileName)
        {
            Product.copyInsert(productId,rename,fileName);
        }

        public static DataTable getBuyRange(int productId)
        {
            return Product.getBuyRange(productId);
        }

        /// <summary>
        /// 根据产品编号和年份查询月平均毛利率
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public static DataSet productMonthGPMlistByProductAndYear(int productId, int year)
        {
            return Product.productMonthGPMlistByProductAndYear(productId, year);
        }

        public static DataTable getSupplyCancelProductList(string productIds)
        {
            return Product.getSupplyCancelProductList(productIds);
        }

        public static DataSet listWithAutoAddPrice(string where, string orderBy, int pageSize, int pageIndex)
        {
            return DAL.DbHelper.ExecuteDataSetPageSize("*", "vw_Product_listWithAutoAddPrice", where, "productId", orderBy, pageSize, pageIndex);
        }

        public static void productUpdateAutoAdd(int adminId, int productId, decimal newAutoPrice)
        {
            DAL.Product.productUpdateAutoAdd(adminId, productId, newAutoPrice);
        }

        public static DataTable listWithPriceByCompanyAndProsuctesSpecials(int companyId, int pros)
        {
            return DAL.Product.listWithPriceByCompanyAndProsuctesSpecials(companyId,pros);
        }

        public static DataSet listWithCurrentPriceAdmin(string where, string orderBy, int pageSize, int pageIndex)
        {
            return DAL.DbHelper.ExecuteDataSetPageSize("productNo,productId,Name,isCurrentCost,newprice,unit,businessNatureName,businessNature", "[dbo].[vw_product_listWithCurrentPrice_Admin]", where, "productId", orderBy, pageSize, pageIndex);
        }

        public static DataTable StatQuantityCountSale(string where, string orderby, int top)
        {
            return DAL.DbHelper.ExecuteDataTable("[dbo].[p_Product_Stat_Quantity_Count_Sale]", where, orderby, top);
        }
        public static DataRow getByProductNo(string ProductNo)
        {
            return Product.getByProductNo(ProductNo);
        }
    }
}
